package com.ruoyi.web.controller.model.controller;

import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.recovery.domain.RecoverySaleMoney;
import com.ruoyi.system.recovery.domain.bo.RecoveryAuditBo;
import com.ruoyi.system.recovery.domain.bo.RecoveryInfoBo;
import com.ruoyi.system.recovery.domain.bo.RecoveryServiceBo;
import com.ruoyi.system.recovery.domain.param.RecoveryInfoParam;
import com.ruoyi.system.recovery.domain.vo.RecoveryInfoVo;
import com.ruoyi.system.recovery.service.IRecoverySaleMoneyService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.recovery.domain.RecoveryInfo;
import com.ruoyi.system.recovery.service.IRecoveryInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 库存
 *
 * @author ruoyi
 * @date 2025-06-04
 */
@RestController
@RequestMapping("/recovery")
public class RecoveryInfoController extends BaseController {
    @Autowired
    private IRecoveryInfoService recoveryInfoService;
    @Autowired
    private IRecoverySaleMoneyService recoverySaleMoneyService;

    /**
     * 查询回收账号列表
     */
    @PreAuthorize("@ss.hasPermi('recovery:recoveryInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(RecoveryInfoParam recoveryInfoParam) {
        startPage();
        List<RecoveryInfoVo> list = recoveryInfoService.selectRecoveryInfoList(recoveryInfoParam);
        return getDataTable(list);
    }

    /**
     * 导出回收账号列表
     */
    @PreAuthorize("@ss.hasPermi('recovery:recoveryInfo:export')")
    @Log(title = "回收账号", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(RecoveryInfoParam recoveryInfo) {
        List<RecoveryInfoVo> list = recoveryInfoService.selectRecoveryInfoList(recoveryInfo);
        ExcelUtil<RecoveryInfoVo> util = new ExcelUtil<RecoveryInfoVo>(RecoveryInfoVo.class);
        return util.exportExcel(list, "回收账号数据");
    }

    /**
     * 获取回收账号详细信息
     */
    @PreAuthorize("@ss.hasPermi('recovery:recoveryInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(recoveryInfoService.selectRecoveryInfoById(id));
    }

    /**
     * 新增回收账号
     */
    @PreAuthorize("@ss.hasPermi('recovery:recoveryInfo:add')")
    @PostMapping
    public AjaxResult add(@RequestBody RecoveryInfo recoveryInfo) {
        boolean success = recoveryInfoService.insertRecoveryInfo(recoveryInfo);
        if (StringUtils.isNotBlank(recoveryInfo.getRemark())) {
            // 添加修改备注记录
            this.editRemark(recoveryInfo);
        }
        return toAjax(success);
    }

    /**
     * 修改账号信息
     */
    @PreAuthorize("@ss.hasPermi('recovery:recoveryInfo:edit')")
    @Log(title = "修改账号信息", businessType = BusinessType.UPDATE, businessId = "#recoveryInfo.id")
    @PutMapping("/editRecoveryInfo")
    public AjaxResult editRecoveryInfo(@RequestBody RecoveryInfo recoveryInfo) {
        return toAjax(recoveryInfoService.editRecoveryInfo(recoveryInfo));
    }

    /**
     * 提交审核
     */
    @PreAuthorize("@ss.hasPermi('recovery:recoveryInfo:edit')")
    @Log(title = "提交审核", businessType = BusinessType.UPDATE, businessId = "#recoveryInfo.id")
    @PutMapping
    public AjaxResult editRecoveryGameInfo(@RequestBody RecoveryInfoBo recoveryInfo) {
        return toAjax(recoveryInfoService.editRecoveryGameInfo(recoveryInfo));
    }

    /**
     * 回收账号审核
     */
    @PreAuthorize("@ss.hasPermi('recovery:recoveryInfo:edit')")
    @PutMapping("/audit")
    public AjaxResult audit(@RequestBody RecoveryAuditBo recoveryAuditBo) {
        if(recoveryAuditBo.isStatus()){
            return toAjax(recoveryInfoService.recoveryApproved(recoveryAuditBo));
        }else{
            return toAjax(recoveryInfoService.recoveryOverrule(recoveryAuditBo));
        }
    }

    /**
     * 回收账号暂卖
     */
    @PreAuthorize("@ss.hasPermi('recovery:recoveryInfo:edit')")
    @PutMapping("/temporarilySold")
    public AjaxResult temporarilySold(@RequestBody RecoveryAuditBo recoveryAuditBo) {
        if(recoveryAuditBo.isStatus()){
            return toAjax(recoveryInfoService.temporarilySold(recoveryAuditBo));
        }else{
            return toAjax(recoveryInfoService.temporarilySoldCancel(recoveryAuditBo));
        }
    }

    /**
     * 修改备注
     */
    @PreAuthorize("@ss.hasPermi('recovery:recoveryInfo:edit')")
    @PutMapping("/editRemark")
    public AjaxResult editRemark(@RequestBody RecoveryInfo recoveryInfo) {
        return toAjax(recoveryInfoService.updateRemark(recoveryInfo.getId(), recoveryInfo.getRemark()));
    }

    /**
     * 删除回收账号
     */
    @PreAuthorize("@ss.hasPermi('recovery:recoveryInfo:remove')")
    @Log(title = "删除回收账号", businessType = BusinessType.DELETE, businessId = "#id")
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(recoveryInfoService.deleteRecoveryInfoById(id));
    }


    /**
     * 修改审核设置
     */
    @PreAuthorize("@ss.hasPermi('recovery:recoveryInfo:edit')")
    @Log(title = "修改审核设置", businessType = BusinessType.UPDATE)
    @PutMapping("/isProcess/{value}")
    public AjaxResult remove(@PathVariable String value) {
        return recoveryInfoService.editIsProcess(value);
    }

    /**
     * 获取历史销售价格
     */
    @PreAuthorize("@ss.hasPermi('recovery:recoveryInfo:query')")
    @GetMapping(value = "/getSaleMoneyHistoryList")
    public TableDataInfo getSaleMoneyHistoryList(RecoverySaleMoney recoverySaleMoney) {
        startPage();
        List<RecoverySaleMoney> list = recoverySaleMoneyService.selectRecoverySaleMoneyList(recoverySaleMoney);
        return getDataTable(list);
    }

    /**
     * 添加售后
     */
    @PreAuthorize("@ss.hasPermi('recovery:recoveryInfo:edit')")
    @Log(title = "添加售后", businessType = BusinessType.UPDATE, businessId = "#recoveryServiceBo.recoveryInfoId")
    @PostMapping("/addServices")
    public AjaxResult addServices(@RequestBody RecoveryServiceBo recoveryServiceBo) {
        return recoveryInfoService.addServices(recoveryServiceBo);
    }

    /**
     * 取消订单
     */
    @PreAuthorize("@ss.hasPermi('recovery:recoveryInfo:edit')")
    @Log(title = "取消订单", businessType = BusinessType.UPDATE, businessId = "#id")
    @PutMapping("/cancleRecovery/{id}")
    public AjaxResult cancleRecovery(@PathVariable Long id) {
        return recoveryInfoService.cancleRecovery(id);
    }

}
